import { OrderModel } from "@models/order.model";
import { OrderRepository } from "@repositories/order.repository";
import { ServiceBase } from "@services/servise.base";
import { SimpleCache } from "@core/cache/simple.cache";

class OrderQueryService extends ServiceBase {
  private static instance: OrderQueryService;
  private repository = new OrderRepository();
  private cache = new SimpleCache<OrderModel>();

  private constructor() {
    super();
  }

  public static getInstance(): OrderQueryService {
    if (!OrderQueryService.instance) {
      OrderQueryService.instance = new OrderQueryService();
    }

    return OrderQueryService.instance;
  }

  protected init(): void {
    async () => {
      await this.loadCache();
    };
  }

  protected concreteDispose(): void {
    this.cache.clear();
  }

  private async loadCache() {
    this.repository.getAll().then((records) => {
      this.cache.addRange(records);
    });
  }
}

export { OrderQueryService };
